<?php
global $_GPC, $_W;
function getcode($code)
{
    $cityinfo = pdo_get('yzzc_sun_city', array('code' => $code));
    return $cityinfo;
}
$platform_fee = pdo_get('yzzc_sun_store_config',['uniacid'=>$_W['uniacid']],['payout_fee','platform_fee']);
$GLOBALS['frames'] = $this->getMainMenu();
$op = $_GPC['op'] ?? 'display';
if ($op == 'display') {
    $where = "where uniacid =" . $_W['uniacid'].' and type=1';
    if ($_GPC['keywords']) {
        $op = $_GPC['keywords'];
        $where .= " and name LIKE  '%$op%' or mobile like '%$op%'";
    }
    if ($_GPC['type'] == 'wait') {
        $where .= " and status =1";
    } elseif ($_GPC['type'] == 'ok') {
        $where .= " and status =2";
    }

    $page = max(1, intval($_GPC['page']));
    $size = intval($_GPC['psize']) ? intval($_GPC['psize']) : 10;
    $sql = 'SELECT * FROM ' . tablename('yzzc_sun_store') . "{$where} ORDER BY store_id DESC LIMIT " . (($page - 1) * $size) . ',' . $size;

    $info = pdo_fetchall($sql);
    foreach ($info as $key => $value) {
        $info[$key]['createtime'] = date('Y-m-d H:i:s', $value['createtime']);
        $province = getcode($value['province']);
        $info[$key]['province'] = $province['fullname'] ?? '';

        $city = getcode($value['city']);
        $info[$key]['city'] = $city['fullname'] ?? '';

        $area = getcode($value['area']);
        $info[$key]['area'] = $area['fullname'] ?? '';
    }
    $total = pdo_fetchcolumn("select count(*) from " . tablename("yzzc_sun_store") . $where);
    $pager = pagination($total, $page, $size);
    include $this->template('web/store/store_list');
} elseif ($op == 'add') {
    $map_key = pdo_get('yzzc_sun_system', array('uniacid' => $_W['uniacid']))['map_key'];
    /**获取省份*/
    $province = pdo_getall('yzzc_sun_city', array('pcode' => 0), array('code', 'fullname'));

    if (checksubmit('submit')) {
        if (empty($_GPC['uid'])) {
            message('会员ID不能为空', '', 'error');
        }

        if (!$user = pdo_get('yzzc_sun_user', ['id' => $_GPC['uid'], 'uniacid' => $_W['uniacid']])) {
            message('会员尚未注册', '', 'error');
        }

        $data = [
            'uid' => $_GPC['uid'],
            'uniacid' => $_W['uniacid'],
            'name' => $_GPC['name'] ?? $user['user_name'],
            'mobile' => $_GPC['mobile'] ?? $user['user_tel'],
            'province' => $_GPC['province'],
            'city' => $_GPC['city'],
            'area' => $_GPC['area'],
            'address' => $_GPC['address'],
            'lng' => $_GPC['lng'],
            'lat' => $_GPC['lat'],
            'id_card_img_front' => $_GPC['id_card_img_front'],
            'id_card_img_side' => $_GPC['id_card_img_side'],
            'pay_status' => 2,
            'createtime' => time()
        ];

        $store = pdo_get('yzzc_sun_store', ['uid' => $_GPC['uid'], 'uniacid' => $_W['uniacid']]);
        if ($store) {
            message('请勿重复申请', $this->createWebUrl('storelist', array()), 'error');
        }

        $result = pdo_insert('yzzc_sun_store', $data);
        if ($result) {
            message('申请成功，等待审核', $this->createWebUrl('storelist', array()), 'success');
        } else {
            message('申请失败', $this->createWebUrl('storelist', array()), 'error');
        }
    }
    include $this->template('web/store/store_add');
} elseif ($op == 'edit') {

    $map_key = pdo_get('yzzc_sun_system', array('uniacid' => $_W['uniacid']))['map_key'];
    /**获取省份*/
    $province = pdo_getall('yzzc_sun_city', array('pcode' => 0), array('code', 'fullname'));

    $info = pdo_get('yzzc_sun_store', ['uniacid' => $_W['uniacid'], 'store_id' => $_GPC['store_id']]);
    $info['province_name'] = getcode($info['province'])['fullname'];
    $info['city_name'] = getcode($info['city'])['fullname'];
    $info['area_name'] = getcode($info['area'])['fullname'];
    $info['pic'] = $info['pic'] ? explode(',',$info['pic']) : '';
    if (checksubmit('submit')) {
        $data = [
            'uniacid' => $_W['uniacid'],
            'uid' => trim($_GPC['uid']),
            'name' => trim($_GPC['name']),
            'mobile' => trim($_GPC['mobile']),
            'province' => trim($_GPC['province']),
            'city' => trim($_GPC['city']),
            'area' => trim($_GPC['area']),
            'lng' => trim($_GPC['lng']),
            'lat' => trim($_GPC['lat']),
            'title' => trim($_GPC['title']),
            'ranges' => trim($_GPC['ranges']),
            'cert_img' => trim($_GPC['cert_img']),
            'pic' => $_GPC['pic'] ? implode(',',$_GPC['pic']) : '',
            'address' => trim($_GPC['address']),
            'service_tel' => trim($_GPC['service_tel']),
            'updatetime' => time(),
        ];
        $result = pdo_update('yzzc_sun_store', $data, [
            'store_id' => $_GPC['store_id'],
            'uniacid' => $_W['uniacid']
        ]);
        if ($result) {
            message('修改成功', $this->createWebUrl('storelist', array('op' => 'display')), 'success');
        } else {
            message('修改失败', $this->createWebUrl('storelist', array('op' => 'edit', 'store_id' => $_GPC['store_id'])), 'error');
        }
    }
    include $this->template('web/store/store_edit');
} elseif ($op == 'delete') {
    $store_info = pdo_get('yzzc_sun_store', array('store_id' => $_GPC['id']));
    $branch = pdo_get('yzzc_sun_branch', array('store_id' => $_GPC['id']));
    if ($store_info['status'] == 1){
        $res1 = pdo_delete('yzzc_sun_store', array('store_id' => $_GPC['id']));
        $res2 = pdo_delete('yzzc_sun_branch', array('store_id' => $_GPC['id']));
        $res3 = pdo_delete('yzzc_sun_branch_admin', array('bid' => $branch['id']));
        if ($res1 && $res3 && $res2) {
            message('操作成功', $this->createWebUrl('storelist', array()), 'success');
        } else {
            message('操作失败', '', 'error');
        }
    }else{
        $res = pdo_delete('yzzc_sun_store', array('store_id' => $_GPC['id']));
        if ($res) {
            message('操作成功', $this->createWebUrl('storelist', array()), 'success');
        } else {
            message('操作失败', '', 'error');
        }
    }
} elseif ($op == 'change') {
    $where = ['store_id' => $_GPC['id']];
    $store_info = pdo_get('yzzc_sun_store', $where,['uid','uniacid','province','city','area','address','lng','lat','ranges','service_tel','mobile','pic','title']);
    if ($store_info['status'] == $_GPC['status']) {
        message('状态错误，修改失败', $this->createWebUrl('storelist', array()), 'error');
    }

    $data = ['status' => $_GPC['status']];
    $platform_fee_ = $payout_fee = 0;
    if (isset($_GPC['remark']) && $_GPC['remark']) {
        $data['remark'] = $_GPC['remark'];
    }else{
        $platform_fee_ = $platform_fee['platform_fee'];
        if (isset($_GPC['platform_fee']) && $_GPC['platform_fee']) {
            $platform_fee_ = $_GPC['platform_fee'];
        }
        $payout_fee = $platform_fee['payout_fee'];
        if (isset($_GPC['payout_fee']) && $_GPC['payout_fee']) {
            $payout_fee = $_GPC['payout_fee'];
        }
    }
    $res = pdo_update('yzzc_sun_store', $data, $where);
    if ($res) {
        if ($data['status'] == 1){
            //审核通过 创建门店
            $uid = $store_info['uid'];
            $store_info['type'] = 2;
            $store_info['status'] = 1;
            $store_info['platform_fee'] = $platform_fee_;
            $store_info['payout_fee'] = $payout_fee;
            $store_info['shop_tel'] = $store_info['mobile'];
            $store_info['name'] = $store_info['title'];
            $store_info['store_id'] = $_GPC['id'];

            unset($store_info['mobile'],$store_info['uid'],$store_info['title']);
            pdo_insert('yzzc_sun_branch',$store_info);
            $branch_id = pdo_insertid();

            //创建超级管理员
            $branch_admin = [
                'bid' => $branch_id,
                'uid' => $uid,
                'auth'=> 1,
                'uniacid' => $store_info['uniacid'],
                'createtime' => time()
            ];
            pdo_insert('yzzc_sun_branch_admin',$branch_admin);
        }

        message('操作成功', $this->createWebUrl('storelist', array()), 'success');
    } else {
        message('操作失败', '', 'error');
    }
}
